package com.geostar.constructionland.attachment.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class GetContentUtils {

  public String getText(String path) {
    String text = "";
    String prefix = path.substring(path.lastIndexOf(".") + 1);
    InputStream in = null;
    // 根据文件不同的类型调用不同的方法
    try {
      in = new FileInputStream(path);
      if ("doc".equals(prefix)) {
        text = getDocx(in);
      } else if ("pdf".equals(prefix)) {
        text = getPdf(path);
      } else if ("txt".equals(prefix)) {
        text = getTxt(in);
      } else if ("xls".equals(prefix)) {
        text = getXls(in);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        in.close();
      } catch (IOException e) {
      }
    }
    return text;
  }

  // 获取xls 文件的内容
  public String getXls(InputStream in) throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(in));

    ExcelExtractor extractor = new ExcelExtractor(wb);
    extractor.setIncludeSheetNames(false);
    extractor.setFormulasNotResults(false);
    extractor.setIncludeCellComments(true);

    String text = extractor.getText();
    System.out.print(text);
    return text;
  }

  // 获取docx 文件的内容
  public String getDocx(InputStream in) throws Exception {
    String text = "";
    XWPFDocument doc = new XWPFDocument(in);
    List<XWPFParagraph> paras = doc.getParagraphs();
    for (XWPFParagraph para : paras) {
      text += para.getText();
    }
    // 获取文档中所有的表格
    List<XWPFTable> tables = doc.getTables();
    List<XWPFTableRow> rows;
    List<XWPFTableCell> cells;
    for (XWPFTable table : tables) {
      rows = table.getRows();
      for (XWPFTableRow row : rows) {
        // 获取行对应的单元格
        cells = row.getTableCells();
        for (XWPFTableCell cell : cells) {
//					logger.info(cell.getText());
        }
      }
    }

    return text;
  }

  // 获取文本 文件的内容
  public String getTxt(InputStream in) throws Exception {
    String text = "";
    InputStreamReader read = new InputStreamReader(in, "UTF-8");// 考虑到编码格式
    BufferedReader bufferedReader = new BufferedReader(read);
    String lineTxt = null;
    while ((lineTxt = bufferedReader.readLine()) != null) {
      text += lineTxt;
    }
    read.close();
    return text;
  }

  // 获取pdf 文件的内容
  public String getPdf(String file) throws Exception {
    String text = "";
    boolean sort = false;
    int startPage = 1;
    int endPage = 10;
    PDDocument document = null;
    try {
      document = PDDocument.load(new File(file));
      PDFTextStripper stripper = new PDFTextStripper();
      stripper.setSortByPosition(sort);
      stripper.setStartPage(startPage);
      stripper.setEndPage(endPage);
      text = stripper.getText(document);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (document != null) {
        document.close();
      }
    }
    return text;
  }
}
